package com.young.shop.dao.impl;

import com.young.shop.dao.ShoppingCartDao;
import com.young.shop.pojo.entity.ShoppingCart;
import com.young.shop.utils.DBManager;

import java.util.List;

public class ShoppingCartDaoImpl implements ShoppingCartDao {
    //根据id获取购物车信息
    @Override
    public ShoppingCart selectShoppingCartById(int id) {
        String sql = "select id, p_id, unit_price, number, u_id, is_available  from shopping_cart where is_available=1 and id=?";
        return DBManager.selectOne(sql,ShoppingCart.class,id);
    }
    //根据用户id查询购物信息
    @Override
    public List<ShoppingCart> selectShoppingCartByUserId(int id) {
        String sql = "select id, p_id, unit_price, number, u_id, is_available  from shopping_cart where is_available=1 and u_id=?";
        return DBManager.selectList(sql,ShoppingCart.class,id);
    }


    //添加购物信息
    @Override
    public int insertShoppingCart(ShoppingCart shoppingCart) {
        String sql = "insert into shopping_cart(id, p_id, unit_price, number, u_id, is_available) values(?,?,?,?,?,?)";
        return DBManager.commonUpdate(sql,shoppingCart.getId(),shoppingCart.getpId(),shoppingCart.getUnitPrice(),shoppingCart.getNumber(),shoppingCart.getuId(),shoppingCart.getIsAvailable());
    }
    //根据用户id和商品id更新购物车信息的数量
    @Override
    public int updateShoppingCartNumber(int userId, String commodityId, int num) {
        String sql = "update shopping_cart set number=? where is_available=1 and u_id=? and p_id=?";
        return DBManager.commonUpdate(sql,num,userId,commodityId);
    }
    //根据用户id和商品id查询购物车信息的总数
    @Override
    public int selectCountByUserIdAndCommodityId(int userId, String commodityId) {
        String sql ="select count(*) from shopping_cart where is_available=1 and u_id=? and p_id=?";
        return DBManager.selectCount(sql,userId,commodityId);
    }
    //根据用户id和商品id删除购物查信息
    @Override
    public int deleteShoppingCart(int userId, String commodity) {
        String sql = "update shopping_cart set is_available=0 where is_available=1 and u_id=? and p_id=?";
        return DBManager.commonUpdate(sql,userId,commodity);
    }

    @Override
    public int selectNumberByUserIdAndCommodityId(int userId, String commodityId) {
        String sql = "select number from shopping_cart where is_available=1 and u_id=? and p_id=?";
        return DBManager.selectCount(sql,userId,commodityId);
    }

}

